package com.audi.adapter;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ClipData;
import android.content.ClipDescription;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.DragShadowBuilder;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.audi.R;



import com.audi.BaseActivity;
import com.audi.activity.DeviceDetailActivity;
import com.audi.activity.HomeControlActivity;
import com.audi.activity.NewActivityPlaceInfo;
import com.audi.adapter.ListDeviceCardAdapterBottom.ViewHolderBottom;
import com.audi.dto.DeviceItem;
import com.audi.message.ActionMessage;
import com.audi.message.SendMessage;
import com.audi.utils.DeviceHelper;
import com.audi.view.DeviceUpdatePhoto;
import com.audi.view.MySlidingDrawer;

public class PlaceCardDevicesAdapter extends BaseAdapter {
	private Activity mActivity;
	private DeviceUpdatePhoto mDeviceUpdatePhoto;
	private ArrayList<DeviceItem> mdeviceCardList;
	// private ImageView imvMenuDeviceCard;
   private Handler Callback;
	public SendMessage sendMessage = new SendMessage();
	private PopupMenu mPopMenu;

	public void setDataForAdapterDevice(ArrayList<DeviceItem> listDevice)
	{
		this.mdeviceCardList= listDevice;
		this.notifyDataSetChanged();
	}
	public PlaceCardDevicesAdapter(Activity activity,
			ArrayList<DeviceItem> devicecradList,
			DeviceUpdatePhoto mDeviceUpdatePhoto) {
		this.mActivity = activity;
		this.mdeviceCardList = devicecradList;
		this.mDeviceUpdatePhoto = mDeviceUpdatePhoto;
	
	}

	@Override
	public int getCount() {
		Log.d("log", "Checkstyle");
		return mdeviceCardList.size();
	}

	@Override
	public DeviceItem getItem(int position) {
		Log.d("log", "Checkstylse2");
		return mdeviceCardList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(int position, View convertView, ViewGroup view) {
		DeviceItem deviceItem = mdeviceCardList.get(position);
		ViewHolderInfoPlace viewholder = null;
		if (convertView == null) {
			viewholder = new ViewHolderInfoPlace();
			convertView = LayoutInflater.from(mActivity).inflate(
					R.layout.row_device_card_in_place_info, null);
			viewholder.nameDevice = (TextView) convertView
					.findViewById(R.id.typeOfDeviceBot_info_place);
			viewholder.frameOnOff = (FrameLayout) convertView
					.findViewById(R.id.frame_onoff_info_place);

			viewholder.txtTemp = (TextView) convertView
					.findViewById(R.id.txt_temp_info_place);

			viewholder.imageMenu = (ImageView) convertView
					.findViewById(R.id.imv_device_tab_menu_bot_info_place);

			viewholder.switchDrawer = (Switch) convertView
					.findViewById(R.id.mySwitch_info_place);

			viewholder.imageWarning = (ImageView) convertView
					.findViewById(R.id.img_warning_info_place);

			viewholder.img1 = (ImageView) convertView
					.findViewById(R.id.imageView100_info_place);
			viewholder.img2 = (ImageView) convertView
					.findViewById(R.id.imageView101_info_place);
			viewholder.imgTemp = (ImageView) convertView
					.findViewById(R.id.img_temp_info_place);
			viewholder.layoutTemp = (FrameLayout) convertView
					.findViewById(R.id.layout_temp_info_place);
			viewholder.imageWarning.setVisibility(View.GONE);

			viewholder.layoutTemp.setVisibility(View.GONE);
			viewholder.frameOnOff.setVisibility(View.GONE);
			viewholder.img1.setVisibility(View.INVISIBLE);
			viewholder.img2.setVisibility(View.INVISIBLE);

			convertView.setTag(viewholder);
		} else {
			viewholder = (ViewHolderInfoPlace) convertView.getTag();
		}
		// set data for listView
		int deviceType = DeviceHelper.getGroupType(deviceItem.getDeviceType());
		if (deviceType == 0) {

			viewholder.imageWarning.setVisibility(View.GONE);

			viewholder.layoutTemp.setVisibility(View.GONE);
			viewholder.frameOnOff.setVisibility(View.VISIBLE);
			// viewholder.slidingDrawer.setVisibility(View.VISIBLE);

			if (deviceItem.getDeviceStatus() == 1) {

				viewholder.switchDrawer.setChecked(false);
				viewholder.switchDrawer
						.setBackgroundResource(R.drawable.icon_decive_on_small_source);
				viewholder.img1.setVisibility(View.VISIBLE);
				viewholder.img2.setVisibility(View.INVISIBLE);
			} else {
				viewholder.switchDrawer.setChecked(true);
				viewholder.switchDrawer
						.setBackgroundResource(R.drawable.icon_device_off_small);

				viewholder.img1.setVisibility(View.INVISIBLE);
				viewholder.img2.setVisibility(View.VISIBLE);
			}
		} else if (deviceType == 1) {

			viewholder.frameOnOff.setVisibility(View.GONE);
			viewholder.imageWarning.setVisibility(View.VISIBLE);
			viewholder.layoutTemp.setVisibility(View.GONE);
			if (deviceItem.getDeviceStatus() == 1) {
				viewholder.imageWarning
						.setImageResource(R.drawable.icon_warning_sensor_green);
			} else {
				viewholder.imageWarning
						.setImageResource(R.drawable.icon_warning_sensor_red);
			}
		} else {

			viewholder.frameOnOff.setVisibility(View.GONE);
			viewholder.imageWarning.setVisibility(View.GONE);
			viewholder.layoutTemp.setVisibility(View.VISIBLE);

			viewholder.imgTemp.setImageResource(R.drawable.temp_icon_small);
			viewholder.txtTemp.setVisibility(View.VISIBLE);
			viewholder.txtTemp.setText(deviceItem.getDeviceStatus() + "");
		}

		viewholder.nameDevice.setText(deviceItem.getDeviceName());

		onclick(viewholder, position, convertView);

		return convertView;
	}

	@SuppressWarnings("deprecation")
	public void onclick(final ViewHolderInfoPlace viewholder,
			final int position, final View convertView) {

		final DeviceItem ItemCurrent = mdeviceCardList.get(position);
		viewholder.switchDrawer
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						// TODO Auto-generated method stub
						if (isChecked == false) {
							Log.d("switch1", "Open");
							viewholder.switchDrawer
									.setBackgroundResource(R.drawable.icon_decive_on_small_source);

							viewholder.img1.setVisibility(View.VISIBLE);
							viewholder.img2.setVisibility(View.INVISIBLE);
							DeviceItem newItemOpen = new DeviceItem();
							newItemOpen.setDeviceId(ItemCurrent.getDeviceId());
							newItemOpen.setDeviceStatus(1);
							sendMessage.sendData(ActionMessage.UPDATE_DEVICE,
									newItemOpen, handlerUpdateDevice);

						} else {
							Log.d("switch2", "Close");
							viewholder.switchDrawer
									.setBackgroundResource(R.drawable.icon_device_off_small);

							viewholder.img1.setVisibility(View.INVISIBLE);
							viewholder.img2.setVisibility(View.VISIBLE);
							DeviceItem newItemOpen = new DeviceItem();
							newItemOpen.setDeviceId(ItemCurrent.getDeviceId());
							newItemOpen.setDeviceStatus(0);
							sendMessage.sendData(ActionMessage.UPDATE_DEVICE,
									newItemOpen, handlerUpdateDevice);
						}
					}
				});

		viewholder.imageMenu.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				mPopMenu = new PopupMenu(mActivity, viewholder.imageMenu);
				mPopMenu.getMenuInflater().inflate(
						R.menu.menu_list_device_card, mPopMenu.getMenu());
				mPopMenu.show();
				mPopMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
					// check style
					@Override
					public boolean onMenuItemClick(MenuItem index) {
						// TODO Auto-generated method stub
						if (index.getItemId() == R.id.deviceMenu_editName) {

							((NewActivityPlaceInfo) mActivity)
									.renameDeviceFuntion(ItemCurrent);
							// check style
							// Toast.makeText(mActivity, "Changed!", 1).show();
						}
						if (index.getItemId() == R.id.deviceMenu_editPlace) {
							// Toast.makeText(mActivity, "Changed!", 1).show();

							((NewActivityPlaceInfo) mActivity)
									.changePlaceDeviceFuntion(ItemCurrent);

						}
						if (index.getItemId() == R.id.deviceMenu_editPhoto) {
							// Toast.makeText(mActivity, "Edited!", 1).show();

							((NewActivityPlaceInfo) mActivity)
									.changePhotoDeviceFuntion(ItemCurrent);

						}
						if (index.getItemId() == R.id.deviceMenu_delete) {

							((NewActivityPlaceInfo) mActivity)
									.deleteDeviceFuntion(ItemCurrent);

						}
						return true;
					}
				});

			}
		});

		viewholder.nameDevice.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				Intent intentDeviceCard = new Intent(mActivity,
						DeviceDetailActivity.class);
				Bundle bundleInfoDevice = new Bundle();

				bundleInfoDevice.putSerializable("item", ItemCurrent);

				intentDeviceCard.putExtras(bundleInfoDevice);
				mActivity.startActivity(intentDeviceCard);

			}
		});

	}

	static class ViewHolderInfoPlace {
		private FrameLayout frameOnOff;
		private TextView nameDevice;
		private ImageView imageMenu;
		private Switch switchDrawer;
		private ImageView imageWarning;
		private FrameLayout layoutTemp;
		private TextView txtTemp;
		private ImageView imgTemp, img1, img2;
	}

	public void swapItems(ArrayList<DeviceItem> mList) {
		this.mdeviceCardList = mList;
		notifyDataSetChanged();
	}
	@SuppressLint("HandlerLeak")
	private Handler handlerUpdateDevice = new Handler() {

		public void handleMessage(final Message msg) {
			mActivity.runOnUiThread(new Runnable() {
				public void run() {
					try {
						if (msg.arg1 != -1) {
							// status
							if (msg.getData()
									.getInt(String
											.valueOf(ActionMessage.UPDATE_DEVICE)) > 0) {
								Log.d("update device", "OK!!!!");
								((BaseActivity) mActivity).onRefreshData();
							}
							

						}
					} catch (Exception e) {
					}
				}
			});
		}
	};
	
}
